<template>
  <div class="inputComponent" v-if="item">
    <div class="title">{{ item.title }}</div>
    <div class="right">
      <el-input
        v-if="item.type === 'number'"
        class="numberSuffix"
        v-model="value"
        :type="item.type"
        :min="item.min"
        :max="item.max"
        :placeholder="item.placeholder"
        @input="textInputChange"
      >
        <template slot="append" v-if="item.suffix && item.suffix !== ''">{{
          item.suffix
        }}</template>
      </el-input>

      <el-input
        v-if="item.type === 'text'"
        :class="item.showWordLimit?'inputComponentWordLimitInput':''"
        v-model="value"
        :type="item.type"
        :maxlength="item.maxlength"
        :placeholder="item.placeholder"
        :show-word-limit="item.showWordLimit"
        clearable
        @input="textInputChange"
        ><template slot="append" v-if="item.suffix && item.suffix !== ''">{{
          item.suffix
        }}</template></el-input
      >

      <el-input
        v-if="item.type === 'textarea'"
        v-model="value"
        :type="item.type"
        :maxlength="item.maxlength"
        :placeholder="item.placeholder"
        :show-word-limit="item.showWordLimit"
        @input="textInputChange"
      ></el-input>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  name: "inputComponent",
  data() {
    return {
      value: "",
      //为了防止页面初始化时，触发不必要的通知更新，此处设置标识符，初始化后2秒置为true
      needUpdate: false,
    };
  },
  created() {},
  props: ["item", "rightIndex", "catalogIndex","ocIndex", "id"],
  components: {},
  mounted() {
    this.$nextTick(() => {
      this.init();
    });
    setTimeout(() => {
      this.needUpdate = true;
    }, 1000);
  },
  methods: {
    //初始化
    init() {
      if (this.item) {
        this.value = null;
        this.$nextTick(() => {
          let oItem = JSON.parse(JSON.stringify(this.item));
          this.value = oItem.value || oItem.default;
        });
      }
    },
    //文字输入时触发的事件
    textInputChange() {
      if (this.needUpdate) {
        //标识是否不需要更新Main中的组件
        let needUpdateMainChild = true;
        if (this.item.ename === "pageName") {
          needUpdateMainChild = false;
        }
        //更新给直属父级
        this.$emit(
          "updateParent",
          JSON.stringify({
            value: this.value,
            ename: this.item.ename,
            rightIndex: this.rightIndex,
            catalogIndex: this.catalogIndex,
            ocIndex: this.ocIndex,
            needUpdateMainChild: needUpdateMainChild,
            needUpdateMain: false,
          })
        );
      }
    },
  },
  computed: mapState(["decorate"]),
  watch: {},
};
</script>
<style scoped lang="scss">
.inputComponent {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  padding: 5px 0px;
  box-sizing: border-box;
  .title {
    width: 100px;
    display: flex;
    justify-content: flex-end;
    padding-right: 10px;
    box-sizing: border-box;
    font-size: 12px;
  }
  .right {
    display: flex;
    align-items: center;
    width: calc(100% - 100px);
    .suffix {
      font-size: 12px;
      display: flex;
      align-items: center;
      color: #999;
      padding-left: 5px;
      box-sizing: border-box;
    }
    .el-input-number {
      width: 140px;
    }
    .numberSuffix {
      width: 140px;
      display: flex;
      align-items: center;
    }
  }
}
</style>
<style lang="scss">
.inputComponentWordLimitInput{
  input.el-input__inner{padding-right:70px;box-sizing:border-box;}
}

</style>